/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package team.gs.model;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import android.content.res.AssetManager;

/**
 * 
 * @author jvm
 */
public class readFile {
	public String readUnicodeFile(AssetManager assetManager,String fileName) throws IOException {
		InputStream in = assetManager.open(fileName);
//		InputStream in = this.getClass().getResourceAsStream(fileName);
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		byte[] tmp = new byte[128];
		int tmpSize = in.read(tmp);
		int i = 0;
		while (tmpSize > 0) {
			bout.write(tmp, 0, tmpSize);
			tmp = new byte[128];

			tmpSize = in.read(tmp);
			i++;
		}
		String result = new String(bout.toByteArray(), "UTF-8");
		in.close();
		return result;
	}

	public String readUnicodeFileSkip(AssetManager assetManager, String fileName, long lskip)
			throws IOException {
		InputStream in = assetManager.open(fileName);
		// InputStream in = this.getClass().getResourceAsStream(fileName);
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		in.skip(lskip);

		byte c;
		final StringBuffer buf = new StringBuffer();
		while ((c = (byte) in.read()) != '@') {
			buf.append((char) c);
		}
		byte[] tmp = new byte[100 * buf.length()];
		int tmpSize = in.read(tmp);
		bout.write(tmp, 0, tmpSize);
		// bout.write(buf,0, 128);
		String result = new String(bout.toByteArray(), "UTF-8");
		in.close();
		return result;
	}

	public static String[] split(String fileContent, String separator) {
		Vector nodes = new Vector();
		// String separator = "\n";
		int index = fileContent.indexOf(separator);
		while (index >= 0) {
			nodes.addElement(fileContent.substring(0, index));
			fileContent = fileContent.substring(index + separator.length());
			index = fileContent.indexOf(separator);
		}
		nodes.addElement(fileContent);
		String[] result = new String[nodes.size()];
		if (nodes.size() > 0) {
			for (int loop = 0; loop < nodes.size(); loop++) {
				result[loop] = (String) nodes.elementAt(loop);
			}

		}
		return result;
	}
}
